Imports IICModel

Namespace PeriodProcessing

    ''' <summary>
    ''' This class handles logic to decide if a certificate was earned
    ''' that has mandatory and optional lessons.
    ''' Example: have a certificate with 2 required Lessons, and 4 optional, 2 of which 
    ''' must be passed.
    ''' </summary>
    Public Class StudentSatisfiedOptionalCertReqs
        Inherits StudentSatisfiedCertReqs
        Implements IStudentSatisfiedCertReqs

        Public Sub New(ByVal period As Period, ByVal student As Student, ByVal certType As CertificationType)
            MyBase.New(period, student, certType)
        End Sub

        ''' <summary>
        ''' checks if all the satisfied requirements have satisified 
        ''' the conditions that the student must fullfill
        ''' in order to earn a certificate
        ''' </summary>
        Public Overrides Function isCertificationEarned() As Boolean

            Dim optionalSatisfied As Integer 'number of optional certifications satisfied
            
            'loop thru each certification requirement of the certificate I am looking to see if the student passed it
            For Each cr As CertificationRequirement In Me.certificationType.CertificationRequirements
                If cr.IsOptional Then
                    'is requirement is optional, just add to the count of optionalSatisfied
                    If Me.studentSatisfiedRequirements.ContainsKey(cr.CertificationRequirementId) Then
                        optionalSatisfied = optionalSatisfied + 1
                    End If

                Else
                    If Me.studentSatisfiedRequirements.ContainsKey(cr.CertificationRequirementId) = False Then
                        'if CertificationRequirementId is not included in the collection of satisifed requirements,
                        'then we just exit and return false
                        If Me.Student.MemberNumber = PeriodProcessor.TRACK_STUDENT_NUMBER Then
                            Trace.TraceInformation("OPT:Member Number: {0} Certificate NOT PASSED: {1}, missing {2}", _
                                             Me.Student.MemberNumber, Me.certificationType.CertificationType, _
                                             cr.LessonCode)
                        End If
                        Return False
                    Else

                    End If
                End If
            Next

            If optionalSatisfied < Me.certificationType.NumberOptionalRequired Then
                Return False
            End If
            If Me.Student.MemberNumber = PeriodProcessor.TRACK_STUDENT_NUMBER Then
                'if we make it here the it means that all CertificationRequirement were satisfied
                Trace.TraceInformation("{0} {1} Member Number: {2} passed **optional** certification: {3}", _
                                 Me.period.PeriodCode, Me.period.DiplomaSuperType.DiplomaSuperTypeCode, _
                                 Me.Student.MemberNumber, Me.certificationType.CertificationType)
            End If

            'set all satisfied requirements' flag isUsedForCertification to true
            Me.studentSatisfiedRequirements.Values.ToList.ForEach(Sub(x)
                                                                      'see DBProcessorPersistor where we use this flag
                                                                      'we use it because we do not want to save 
                                                                      'ExceptionSatisfiedRequirements unless they are 
                                                                      'actually used for a certification
                                                                      x.isUsedForCertification = True

                                                                  End Sub)



            Return True

        End Function

    End Class
End Namespace
